<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>

<table class="centering">
    <tr><td>
            <% System.gc();%>
            
            <% if (request.getParameter("page") == null || session.getAttribute("user") == null) {%>
            
            <h2>Unauthorized entrance.</h2>
            
            <% } else if (request.getParameter("username") == null) {%>
            
            <script type="text/javascript" src="login.js"></script>
            <div style="text-align:left;"><form action="index.jsp?page=login" method="post" onsubmit="return checkform(this)">
                    <table class="nice">
                        <tr><td class="b">Username:</td><td>&nbsp;&nbsp;&nbsp;<input id="firstFocus" type="text" name="username" /></td></tr>
                        <tr><td class="b">Password:</td><td>&nbsp;&nbsp;&nbsp;<input type="password" name="password" /></td></tr>
                        <tr><td colspan="2" align="center"><br /><br /><input type="submit" value="Log In" /></td></tr>
                    </table>
            </form></div>
            
            
            <% } else {
     if (request.getMethod().equals("POST")) {
         User u = (User) request.getSession().getAttribute("user");
         UserManager db = new UserManager(DBInfo.getConnection());

         if (!db.isConnected()) {
             out.print("<h2 class=\"nice\">No connection with the database.</h2>");
         } else if (!db.usernameExists(request.getParameter("username"))) {
             out.print("<h2 class=\"nice\">User with username " + request.getParameter("username") + " doesn't exist.</h2>");
         } else if (u.Login(request.getParameter("username"), request.getParameter("password"))) {
             out.print("<h2 class=\"nice\">Logged in succesfully.</h2>");
             response.sendRedirect("index.jsp");
         } else {
             out.print("<h2 class=\"nice\">Wrong password.</h2>");
         }
     }
 }
            %>
    </td></tr>
</table>